package com.example.shishaolong.warehousemanager.dao;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.shishaolong.warehousemanager.common.DateUtil;
import com.example.shishaolong.warehousemanager.db.WarehouseDBHelper;
import com.example.shishaolong.warehousemanager.entity.AdminEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 时少龙
 */
public class AdminDao {
    WarehouseDBHelper helper = null;

    public AdminDao(Context context) {
        //在构造方法中实例化的好处是，
        //当实例化AdminDao时，数据库DbHelper也进行了实例化
        helper = new WarehouseDBHelper(context, "warehouse.db", null, 1);
    }

    /**
     * 添加管理员方法
     *
     * @param adminEntity
     */
    public void add(AdminEntity adminEntity) {
        SQLiteDatabase db = helper.getWritableDatabase();
        db.execSQL("insert into admin_info(name,password,sex,level,add_date,modify_date) values(?,?,?,?,?,?)",
                new Object[]{adminEntity.getName(), adminEntity.getPassword(), adminEntity.getSex(),
                        adminEntity.getLevel(), DateUtil.getCurrentDateStr(), DateUtil.getCurrentDateStr()});
        db.close();
    }

    /**
     * @param id 通过id删除一个管理员
     */
    public void delete(int id) {
        SQLiteDatabase db = helper.getWritableDatabase();
        db.execSQL("delete from admin_info where id=?", new Object[]{id});
        db.close();
    }

    /**
     * 删除全部管理员
     */
    public void deleteAll() {
        SQLiteDatabase db = helper.getWritableDatabase();
        db.execSQL("delete from admin_info");
        db.close();
    }

    /**
     * 更新管理员信息
     *
     * @param adminEntity 管理员实体类
     */
    public void update(AdminEntity adminEntity) {
        SQLiteDatabase db = helper.getWritableDatabase();
        db.execSQL("update admin_info set name=?,password=?,sex=?,level=?,add_date=?,modify_date=? where id=?",
                new Object[]{adminEntity.getName(), adminEntity.getPassword(), adminEntity.getSex(),
                        adminEntity.getLevel(), adminEntity.getAddDate(), DateUtil.getCurrentDateStr(), adminEntity.getId()});
        db.close();
    }

     /**
     *  根据用户名和密码查询管理员信息
     * @param userName 用户名
     * @param userPassword 密码
     * @return
     */
    public AdminEntity findByNameAndPwd(String userName, String userPassword) {
        AdminEntity adminEntity = null;
        SQLiteDatabase db = helper.getReadableDatabase();

        Cursor cursor = db.rawQuery("select * from admin_info where name=? and password=?",
                new String[]{userName,userPassword});
        if (cursor.moveToFirst()) {
            int id = cursor.getInt(cursor.getColumnIndex("id"));
            String name = cursor.getString(cursor.getColumnIndex("name"));
            String password = cursor.getString(cursor.getColumnIndex("password"));
            int sex = cursor.getInt(cursor.getColumnIndex("sex"));
            int leve = cursor.getInt(cursor.getColumnIndex("level"));
            String addDate = cursor.getString(cursor.getColumnIndex("add_date"));
            String modifyDate = cursor.getString(cursor.getColumnIndex("modify_date"));

            adminEntity = new AdminEntity();
            adminEntity.setId(id);
            adminEntity.setName(name);
            adminEntity.setPassword(password);
            adminEntity.setSex(sex);
            adminEntity.setLevel(leve);
            //TODO 日期时间类型转化 addDate
            System.out.println("addDate:" + addDate + ",modifyDate:" + modifyDate);
            adminEntity.setAddDate(addDate);
            //TODO  日期转换： modify
            adminEntity.setModifyDate(modifyDate);
        }
        return adminEntity;
    }

    /**
     * 根据id查询管理员信息
     *
     * @param id
     * @return
     */
    public AdminEntity findById(int id) {
        AdminEntity adminEntity = null;
        SQLiteDatabase db = helper.getReadableDatabase();

        Cursor cursor = db.rawQuery("select * from admin_info where id=?",
                new String[]{"" + id});
        if (cursor.moveToFirst()) {
            String name = cursor.getString(cursor.getColumnIndex("name"));
            String password = cursor.getString(cursor.getColumnIndex("password"));
            int sex = cursor.getInt(cursor.getColumnIndex("sex"));
            int leve = cursor.getInt(cursor.getColumnIndex("level"));
            String addDate = cursor.getString(cursor.getColumnIndex("add_date"));
            String modifyDate = cursor.getString(cursor.getColumnIndex("modify_date"));

            adminEntity = new AdminEntity();
            adminEntity.setId(id);
            adminEntity.setName(name);
            adminEntity.setPassword(password);
            adminEntity.setSex(sex);
            adminEntity.setLevel(leve);
            //TODO 日期时间类型转化 addDate
            System.out.println("addDate:" + addDate + ",modifyDate:" + modifyDate);
            adminEntity.setAddDate(addDate);
            //TODO  日期转换： modify
            adminEntity.setModifyDate(modifyDate);
        }
        return adminEntity;
    }

    /**
     * @return 返回所有的管理员信息
     */
    public List<AdminEntity> findAll() {
        List<AdminEntity> list = new ArrayList<>();
        SQLiteDatabase db = helper.getReadableDatabase();

        Cursor cursor = db.rawQuery("select * from admin_info ", null);
        while (cursor.moveToNext()) {

            int id = cursor.getInt(cursor.getColumnIndex("id"));
            String name = cursor.getString(cursor.getColumnIndex("name"));
            String password = cursor.getString(cursor.getColumnIndex("password"));
            int sex = cursor.getInt(cursor.getColumnIndex("sex"));
            int leve = cursor.getInt(cursor.getColumnIndex("level"));
            String addDate = cursor.getString(cursor.getColumnIndex("add_date"));
            String modifyDate = cursor.getString(cursor.getColumnIndex("modify_date"));

            AdminEntity adminEntity = new AdminEntity();
            adminEntity.setId(id);
            adminEntity.setName(name);
            adminEntity.setPassword(password);
            adminEntity.setSex(sex);
            adminEntity.setLevel(leve);
            //TODO 日期时间类型转化 addDate
            System.out.println("addDate:" + addDate + ",modifyDate:" + modifyDate);
            adminEntity.setAddDate(addDate);
            //TODO  日期转换： modify
            adminEntity.setModifyDate(modifyDate);
            list.add(adminEntity);
        }
        db.close();
        return list;
    }
}
